package com.lyb.spzx.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lyb.spzx.manager.mapper.ProductDetailsMapper;
import com.lyb.spzx.manager.mapper.ProductMapper;
import com.lyb.spzx.manager.mapper.ProductSkuMapper;
import com.lyb.spzx.manager.service.ProductService;
import com.lyb.spzx.model.dto.product.ProductDto;
import com.lyb.spzx.model.entity.product.Product;
import com.lyb.spzx.model.entity.product.ProductDetails;
import com.lyb.spzx.model.entity.product.ProductSku;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ProductServiceImpl implements ProductService {

    @Autowired
    ProductMapper productMapper;

    @Autowired
    ProductSkuMapper productSkuMapper;

    @Autowired
    ProductDetailsMapper productDetailsMapper;


    @Override
    public PageInfo<Product> findAllProductByPage(Integer pageNum, Integer pageSize, ProductDto productDto) {
        PageHelper.startPage(pageNum,pageSize);
        List<Product> products = productMapper.findAllProduct(productDto);
        return new PageInfo<>(products);
    }

    @Override
    public void saveProduct(Product product) {
        //1、保存商品基本信息
        product.setStatus(0); //添加默认上架状态
        product.setAuditStatus(0); //添加默认审核状态
        System.out.println("product = " + product);
        productMapper.saveProduct(product);
        //2、保存商品sku信息
        List<ProductSku> productSkuList = product.getProductSkuList();
        int size = productSkuList.size();
        for (int i = 0; i < size; i++) {
            ProductSku productSku = productSkuList.get(i);
            //构建skuCode
            productSku.setSkuCode(productSku.getProductId() + "_" + i);
            productSku.setProductId(product.getId());
            productSku.setSkuName(productSku.getSkuName() + productSku.getSkuSpec());
            productSku.setSaleNum(0); //设置销量
            productSku.setStatus(0);

            productSkuMapper.saveSku(productSku);
        }
        //3、保存商品详情信息
        ProductDetails productDetails = new ProductDetails();
        productDetails.setProductId(product.getId());
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetailsMapper.saveDetails(productDetails);
    }

    @Override
    public Product getById(Long id) {
        //根据id查询商品信息
        Product product = productMapper.selectById(id);
        //根据商品id查询商品sku信息
        List<ProductSku> productSkus = productSkuMapper.selectByProductId(id);
        product.setProductSkuList(productSkus);
        //根据商品id查询商品详情信息
        ProductDetails productDetails = productDetailsMapper.selectByProductId(id);
        product.setDetailsImageUrls(productDetails.getImageUrls());
        return product;
    }

    @Override
    public void updateById(Product product) {
        //修改商品的基本信息
        productMapper.updateById(product);
        //修改商品sku信息
        List<ProductSku> productSkuList = product.getProductSkuList();
        productSkuList.forEach(productSku -> {
            productSkuMapper.udateById(productSku);
        });

        //修改商品详情信息
        ProductDetails productDetails = productDetailsMapper.selectByProductId(product.getId());
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetailsMapper.updateById(productDetails);
    }

    @Override
    public void deleteById(Long id) {
        //根据id删除商品基本信息
        productMapper.deleteById(id);
        //根据商品id删除商品sku信息
        productSkuMapper.deleteByProductId(id);
        //根据商品id删除商品详情信息
        productDetailsMapper.deleteByProductId(id);
    }

    //审核
    @Override
    public void updateAuditStatus(Long id, Integer auditStatus) {
        Product product = new Product();
        product.setId(id);
        if (auditStatus == 1){
            product.setAuditStatus(1);
            product.setAuditMessage("审核通过");
        }else {
            product.setAuditStatus(-1);
            product.setAuditMessage("审核拒绝");
        }
        productMapper.updateById(product);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        Product product = new Product();
        product.setId(id);
        if (status == 1){
            product.setStatus(1);
        }else {
            product.setStatus(-1);
        }
        productMapper.updateById(product);
    }
}
